<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Gass_message_rest extends CI_Controller {
  function __construct() 
  {
    parent::__construct();   	
		// Load required CI libraries and helpers.
		//$this->load->database();
		$this->load->library('session');
 		$this->load->helper('url');
 		$this->load->library('restrequest');
 		$this->load->library('restutils');
 		//$this->load->helper('form');
 		
    //$this->auth = new stdClass;
		//$this->load->library('flexi_auth');	
		$this->data = null;
		//调试使用
		//$this->output->enable_profiler(TRUE);
  }
  
  function get(){
  	$data = Restutils::processRequest(); 

    switch($data->getMethod){
	  // this is a request for all users, not one in particular
	  case 'get':
		  $user_list = array('asdf','qwer','wert'); // assume this returns an array

		  if($data->getHttpAccept == 'json'){
		  	RestUtils::sendResponse(200, json_encode($user_list), 'application/json');
		  }else if ($data->getHttpAccept == 'xml')	{
			  // using the XML_SERIALIZER Pear Package
			  $options = array
			  (  
				  'indent' => '     ',
				  'addDecl' => false,
				  'rootName' => $fc->getAction(),
				  XML_SERIALIZER_OPTION_RETURN_RESULT => true
			   );
			  $serializer = new XML_Serializer($options);

			  RestUtils::sendResponse(200, $serializer->serialize($user_list), 'application/xml');
		  }

		  break;
	  // new user create
	case 'post':
		$user = new User();
		$user->setFirstName($data->getData()->first_name);  // just for example, this should be done cleaner
		// and so on...
		$user->save();

		// just send the new ID as the body
		RestUtils::sendResponse(201, $user->getId());
		break;
    }

		//$this->data['message'] = (! isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		
    //$this->load->view('demo_dashboard_view', $this->data);
 	}  
}